package com.ctyon.watchmanager.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.launcher.ARouter;
import com.ctyon.common.event.RemoveWatch;
import com.ctyon.common.util.SPUtils;
import com.ctyon.common.util.TSnackbarUtil;
import com.ctyon.netlibrary.net.revert.BaseResponseEntity;
import com.ctyon.netlibrary.use.BaseObserver;
import com.ctyon.watchmanager.BasicApp;
import com.ctyon.watchmanager.R;
import com.ctyon.watchmanager.databinding.FragmentBusinessBinding;
import com.ctyon.watchmanager.model.net.service.NetService;
import com.ctyon.watchmanager.present.BusinessPresenter;
import com.ctyon.watchmanager.ui.BaseFragment;
import com.ctyon.watchmanager.ui.activity.BusinessActivity;
import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.EventBus;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.databinding.DataBindingUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import static com.ctyon.common.container.ARouterPath.ROUTER_ACTIVITY_CONTAINER;
import static com.ctyon.common.util.SPUtils.WATCH_ID;
import static com.ctyon.watchmanager.ui.activity.ContainerActivity.TAB_INDEX_PARAM;

/**
 * CreateDate：19-6-25 on 上午9:13
 * Describe:
 * Coder: lee
 */
public class BusinessFragment extends BaseFragment {

    public static final String TAG = "BusinessFragment";

    private FragmentBusinessBinding mBinding;
    private AlertDialog.Builder builder;

    public static BusinessFragment newInstance() {
        final BusinessFragment fragment = new BusinessFragment();
        final Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_business, container, false);

        mBinding.setBusinessPresenter(presenter);

        return mBinding.getRoot();

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mBinding.setNick(BasicApp.getInstance().getWatch().getNick());
    }

    private final BusinessPresenter presenter = new BusinessPresenter() {
        @Override
        public void onBackClick() {
            ((BusinessActivity) getActivity()).finish();
        }

        @Override
        public void onFlowerClick() {//爱心奖励
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, FlowerFragment.TAG)
                    .navigation();
        }

        @Override
        public void onDisableClick() {//上课禁用
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, DisableFragment.TAG)
                    .navigation();
        }

        @Override
        public void onSosClick() {//Sos
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, SosFragment.TAG)
                    .navigation();
        }

        @Override
        public void onAlarmClick() {//设备闹钟
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, AlarmFragment.TAG)
                    .navigation();
        }

        @Override
        public void onSmsClick() {//短信提醒
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, SmsFragment.TAG)
                    .navigation();
        }

        @Override
        public void onLocationClick() {//陌生电话开关
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, StrangeCallFragment.TAG)
                    .navigation();
        }

        @Override
        public void onPowerClick() {//远程关机
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, PowerFragment.TAG)
                    .navigation();
        }

        @Override
        public void onMessageClick() {//设备消息
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, MessageFragment.TAG)
                    .navigation();
        }

        @Override
        public void onCameraClick() {//远程拍照
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, CameraFragment.TAG)
                    .navigation();
        }

        @Override
        public void onMoneyClick() {//话费流量
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, MoneyFragment.TAG)
                    .navigation();
        }

        @Override
        public void onVideoClick() {//视屏聊天
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, VideoFragment.TAG)
                    .navigation();
        }

        @Override
        public void onProgramClick() {//程序控制
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, ProgramFragment.TAG)
                    .navigation();
        }

        @Override
        public void onStepClick() {//计步
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, StepFragment.TAG)
                    .navigation();
        }

        @Override
        public void onIntervalClick() {//心跳间隔
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, IntervalFragment.TAG)
                    .navigation();
        }

        @Override
        public void onWatchClick() {//手表详情
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, WatchFragment.TAG)
                    .navigation();
        }

        @Override
        public void onCallClick() {//拨打电话
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, CallFragment.TAG)
                    .navigation();
        }

        @Override
        public void onRemoveClick() {
            showDialog();
        }

        @Override
        public void onIconClick() {
            ARouter.getInstance().build(ROUTER_ACTIVITY_CONTAINER)
                    .withString(TAB_INDEX_PARAM, WatchFragment.TAG)
                    .navigation();
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        Logger.t(this.getClass().getName()).d("onDestroy");
    }

    private void showDialog() {
        if (null == builder) {
            builder = new AlertDialog.Builder(getActivity(), R.style.MyDialog);
            builder.setTitle("解绑手表")
                    .setMessage("确定要解绑手表吗?解绑后手表将会恢复出厂设置！")
                    .setPositiveButton("确定", (dialog, which) -> {
                        NetService.getInstance()
                                .removeWatch(BasicApp.getInstance().getManager().getManagerId(), BasicApp.getInstance().getWatch().getMeid())
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new BaseObserver<BaseResponseEntity>() {
                                    @Override
                                    public void onSuccess(BaseResponseEntity responseEntity) {
                                        Logger.d("onSuccess : " + responseEntity.toString());
                                        TSnackbarUtil.TSBSuccess(getActivity(), "解绑成功");
                                        SPUtils.getInstance().remove(WATCH_ID);
                                        EventBus.getDefault().post(new RemoveWatch());
                                        getActivity().finish();
                                    }
                                });
                    });
        }
        builder.show();
    }

}
